﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public class OnePrjOneDiscus
    {

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus model = new ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus();
            if (row != null)
            {
                if (row["FOnePrjOneDiscusID"] != null && row["FOnePrjOneDiscusID"].ToString() != "")
                {
                    model.FOnePrjOneDiscusID = decimal.Parse(row["FOnePrjOneDiscusID"].ToString());
                }
                if (row["FTitle"] != null)
                {
                    model.FTitle = row["FTitle"].ToString();
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FisPoor"] != null)
                {
                    model.FisPoor = row["FisPoor"].ToString();
                }
                if (row["FAttachOldFileName"] != null)
                {
                    model.FAttachOldFileName = row["FAttachOldFileName"].ToString();
                }
                if (row["FAttachSvrFileName"] != null)
                {
                    model.FAttachSvrFileName = row["FAttachSvrFileName"].ToString();
                }
                if (row["FAttachFileExt"] != null)
                {
                    model.FAttachFileExt = row["FAttachFileExt"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FInsEmpNo"] != null)
                {
                    model.FInsEmpNo = row["FInsEmpNo"].ToString();
                }
                if (row["FUpdEmpNo"] != null)
                {
                    model.FUpdEmpNo = row["FUpdEmpNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row["FCheckEmpNo"] != null)
                {
                    model.FCheckEmpNo = row["FCheckEmpNo"].ToString();
                }
                if (row["FCheckDate"] != null && row["FCheckDate"].ToString() != "")
                {
                    model.FCheckDate = DateTime.Parse(row["FCheckDate"].ToString());
                }
                if (row["FCheckIP"] != null)
                {
                    model.FCheckIP = row["FCheckIP"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FDeptName"] != null && row["FDeptName"].ToString() != "")
                {
                    model.FDeptName = row["FDeptName"].ToString();              
                }

            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus GetModel(decimal FOnePrjOneDiscusID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FOnePrjOneDiscusID,FTitle,FYear,FDeptNo,FEntityNo,FNote,FisPoor,FAttachOldFileName,FAttachSvrFileName,FAttachFileExt,FInsDate,FInsIP,FInsEmpNo,FUpdEmpNo,FUpdDate,FUpdIP,FCheckEmpNo,FCheckDate,FCheckIP,FStatus,FDeptName from v_OnePrjOneDiscus ");
            strSql.Append(" where FOnePrjOneDiscusID=@FOnePrjOneDiscusID");
            SqlParameter[] parameters = {
					new SqlParameter("@FOnePrjOneDiscusID", SqlDbType.Decimal)
			};
            parameters[0].Value = FOnePrjOneDiscusID;

            ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus model = new ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_OnePrjOneDiscus(");
            strSql.Append("FTitle,FYear,FDeptNo,FEntityNo,FNote,FisPoor,FAttachOldFileName,FAttachSvrFileName,FAttachFileExt,FInsDate,FInsIP,FInsEmpNo,FUpdEmpNo,FUpdDate,FUpdIP,FCheckEmpNo,FCheckDate,FCheckIP,FStatus)");
            strSql.Append(" values (");
            strSql.Append("@FTitle,@FYear,@FDeptNo,@FEntityNo,@FNote,@FisPoor,@FAttachOldFileName,@FAttachSvrFileName,@FAttachFileExt,@FInsDate,@FInsIP,@FInsEmpNo,@FUpdEmpNo,@FUpdDate,@FUpdIP,@FCheckEmpNo,@FCheckDate,@FCheckIP,@FStatus)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,255),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FisPoor", SqlDbType.NVarChar,1),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckDate", SqlDbType.DateTime),
					new SqlParameter("@FCheckIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1)};
            parameters[0].Value = model.FTitle;
            parameters[1].Value = model.FYear;
            parameters[2].Value = model.FDeptNo;
            parameters[3].Value = model.FEntityNo;
            parameters[4].Value = model.FNote;
            parameters[5].Value = model.FisPoor;
            parameters[6].Value = model.FAttachOldFileName;
            parameters[7].Value = model.FAttachSvrFileName;
            parameters[8].Value = model.FAttachFileExt;
            parameters[9].Value = model.FInsDate;
            parameters[10].Value = model.FInsIP;
            parameters[11].Value = model.FInsEmpNo;
            if (model.FUpdEmpNo == "")
            {
                parameters[12].Value = DBNull.Value;
            }
            else {
                parameters[12].Value = model.FUpdEmpNo;
            }

            if (model.FUpdDate == null)
            {
                parameters[13].Value = DBNull.Value;
            }
            else
            {
                parameters[13].Value = model.FUpdDate;
            }
            if (model.FUpdIP == "")
            {
                parameters[14].Value = DBNull.Value;
            }
            else
            {
                parameters[14].Value = model.FUpdIP;
            }
            if (model.FCheckEmpNo == "")
            {
                parameters[15].Value = DBNull.Value;
            }
            else
            {
                parameters[15].Value = model.FCheckEmpNo;
            }

            if (model.FCheckDate == null)
            {
                parameters[16].Value = DBNull.Value;
            }
            else
            {
                parameters[16].Value = model.FCheckDate;
            }
            if (model.FCheckIP == "")
            {
                parameters[17].Value = DBNull.Value;
            }
            else
            {
                parameters[17].Value = model.FCheckIP;
            }
            if (model.FStatus == "")
            {
                parameters[18].Value = DBNull.Value;
            }
            else
            {
                parameters[18].Value = model.FStatus;
            }

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_OnePrjOneDiscus set ");
            strSql.Append("FTitle=@FTitle,");
            strSql.Append("FYear=@FYear,");
            strSql.Append("FNote=@FNote,");
            strSql.Append("FisPoor=@FisPoor,");
            strSql.Append("FAttachOldFileName=@FAttachOldFileName,");
            strSql.Append("FAttachSvrFileName=@FAttachSvrFileName,");
            strSql.Append("FAttachFileExt=@FAttachFileExt,");
            strSql.Append("FInsDate=@FInsDate,");
            strSql.Append("FInsIP=@FInsIP,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FCheckDate=@FCheckDate,");
            strSql.Append("FUpdEmpNo=@FUpdEmpNo,");
            strSql.Append("FCheckIP=@FCheckIP,");
            strSql.Append("FDeptNo=@FDeptNo,");
            strSql.Append("FStatus=@FStatus");
            strSql.Append(" where FOnePrjOneDiscusID=@FOnePrjOneDiscusID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,255),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FNote", SqlDbType.NVarChar,-1),
					new SqlParameter("@FisPoor", SqlDbType.NVarChar,1),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckDate", SqlDbType.DateTime),
					new SqlParameter("@FCheckIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FOnePrjOneDiscusID", SqlDbType.Decimal,13),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FCheckEmpNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FTitle;
            parameters[1].Value = model.FYear;
            parameters[2].Value = model.FNote;
            parameters[3].Value = model.FisPoor;
            parameters[4].Value = model.FAttachOldFileName;
            parameters[5].Value = model.FAttachSvrFileName;
            parameters[6].Value = model.FAttachFileExt;
            parameters[7].Value = model.FInsDate;
            parameters[8].Value = model.FInsIP;
            parameters[9].Value = model.FUpdDate;
            parameters[10].Value = model.FUpdIP;
            if (model.FCheckDate == null)
            {
                parameters[11].Value = DBNull.Value;
            }
            else
            {
                parameters[11].Value = model.FCheckDate;
            
            }
            if (model.FCheckIP == "")
            {
                parameters[12].Value = DBNull.Value;
            }
            else {
                parameters[12].Value = model.FCheckIP;
            }
            if (model.FStatus == "")
            {
                parameters[13].Value = DBNull.Value;
            }
            else {
                parameters[13].Value = model.FStatus;
            }
            
            parameters[14].Value = model.FOnePrjOneDiscusID;
            parameters[15].Value = model.FDeptNo;
            parameters[16].Value = model.FEntityNo;
            parameters[17].Value = model.FInsEmpNo;
            parameters[18].Value = model.FUpdEmpNo;
            if (model.FCheckEmpNo == "")
            {
                parameters[19].Value = DBNull.Value;
            }
            else
            {
                parameters[19].Value = model.FCheckEmpNo;
            }
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="idArray"></param>
        /// <param name="entityNo"></param>
        public void DeleteMany(string[] idArray)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            if (idArray.Length > 0)
            {
                //删除广告数据
                strSql = new StringBuilder();
                strSql.Append("delete from dbo.t_OnePrjOneDiscus ");
                strSql.Append(" where FOnePrjOneDiscusID=@FOnePrjOneDiscusID ");

                string strSqlAd = strSql.ToString();

                foreach (string id in idArray)
                {
                    cmds = new CommandInfo();

                    SqlParameter[] parameters = {
					    new SqlParameter("@FOnePrjOneDiscusID", SqlDbType.Decimal,13)};

                    parameters[0].Value = id;

                    cmds.CommandText = strSqlAd;
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);
                }

            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList);

        
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="fOnePrjOneDiscusID"></param>
        /// <returns></returns>
        public bool Delete(decimal fOnePrjOneDiscusID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_OnePrjOneDiscus ");
            strSql.Append(" where FOnePrjOneDiscusID=@FOnePrjOneDiscusID");
            SqlParameter[] parameters = {
					new SqlParameter("@FOnePrjOneDiscusID", SqlDbType.Decimal)
			};
            parameters[0].Value = fOnePrjOneDiscusID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获得多条图片存放路径
        /// </summary>
        /// <param name="strAdCds">编号字符</param>
        /// <param name="strEntityNo">公司编号</param>
        /// <returns>List集合</returns>
        public List<string> GetSrvName(string id, string strEntityNo)
        {
            List<string> listStr = new List<string>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAttachSvrFileName");
            strSql.Append(" FROM dbo.t_OnePrjOneDiscus where FOnePrjOneDiscusID in('");
            strSql.Append(id);
            strSql.Append("')");
            strSql.Append(" and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.VarChar,50)};
            parameters[0].Value = strEntityNo;
            using (SqlDataReader sdr = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters))
            {
                while (sdr.Read())
                {
                    listStr.Add(sdr[0].ToString());
                }
            }

            return listStr;
        }

        /// <summary>
        /// 获得多条图片存放路径
        /// </summary>
        /// <param name="strAdCds">编号字符</param>
        /// <param name="strEntityNo">公司编号</param>
        /// <returns>集合</returns>
        public List<string> GetSrvName(string[] ids, string strEntityNo)
        {
            List<string> listStr = new List<string>();
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[] { };
            if (ids.Length > 0)
            {
                strSql.Append("select FAttachSvrFileName");
                strSql.Append(" FROM dbo.t_OnePrjOneDiscus  where FOnePrjOneDiscusID in");
                strSql.Append("(");
                for (int i = 0; i < ids.Length; i++)
                {
                    if (i + 1 == ids.Length)
                    {
                        strSql.Append("'" + ids[i] + "'");
                    }
                    else
                    {
                        strSql.Append("'" + ids[i] + "'" + ",");
                    }

                }
                strSql.Append(")");
                strSql.Append(" and FEntityNo=@FEntityNo ");

                foreach (string adNo in ids)
                {
                    parameters = new SqlParameter[] {
					    new SqlParameter("@FEntityNo", SqlDbType.VarChar,50)};
                    parameters[0].Value = strEntityNo;
                }
                DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    listStr.Add(dr["FAttachSvrFileName"].ToString());
                }

            }
            return listStr;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Check(ElingOA6_Admin_DSJCJ.Model.OnePrjOneDiscus model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_OnePrjOneDiscus set ");
            strSql.Append("FCheckEmpNo=@FCheckEmpNo,");
            strSql.Append("FCheckDate=@FCheckDate,");
            strSql.Append("FCheckIP=@FCheckIP,");
            strSql.Append("FStatus=@FStatus");
            strSql.Append(" where FOnePrjOneDiscusID=@FOnePrjOneDiscusID");
            SqlParameter[] parameters = {
					new SqlParameter("@FCheckDate", SqlDbType.DateTime),
					new SqlParameter("@FCheckIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FOnePrjOneDiscusID", SqlDbType.Decimal,13),
					new SqlParameter("@FCheckEmpNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FCheckDate;
            parameters[1].Value = model.FCheckIP;
            parameters[2].Value = model.FStatus;
            parameters[3].Value = model.FOnePrjOneDiscusID;
            parameters[4].Value = model.FCheckEmpNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
